/**
 * Created by ljg on 2017/2/22.
 */
export default function () {
    angular.module('ui.table')
        .directive('tablePage', function (eventHelper) {
            return {
                restrict: 'E',
                replace: true,
                transclude: true,
                controller:function ($scope) {
                    $scope.$on("ui.table.getData",function (event,data,param) {
                        const total=param.totalCount;
                        const pageSize= param.pageSize;
                        const pageNo= param.pageNo;
                        const totalPage=Math.ceil(total/pageSize);

                        const pageNums=[1,2,3,4,5,6,7];//遍历的分页页数
                        if(totalPage>0){
                            if(totalPage<8){
                                pageNums.splice(totalPage)
                            }else{
                                if(pageNo<=3){
                                    pageNums[5]="...";
                                    pageNums[6]=totalPage;
                                }else if(totalPage-pageNo<=3){
                                    pageNums[1]="...";
                                    pageNums[2]=totalPage-4;
                                    pageNums[3]=totalPage-3;
                                    pageNums[4]=totalPage-2;
                                    pageNums[5]=totalPage-1;
                                    pageNums[6]=totalPage;

                                }else{
                                    pageNums[1]=".. ";
                                    pageNums[5]=" ..";
                                    pageNums[6]=totalPage;
                                    pageNums[2]=pageNo;
                                    pageNums[3]=pageNo+1;
                                    pageNums[4]=pageNo+2;
                                }
                            }
                        }else {
                            pageNums.splice(0);

                        }
                        $scope.totalPage=totalPage;
                        $scope.selectedPage=pageNo;//选中当前页码
                        $scope.pageNums=pageNums;//展示分页页数1
                        $scope.$apply();

                    });

                },
                require:"^?uiContainer",
                scope:{},
                template: function(el,attr){
                    return '<div class="row">'+
                        '<div class="col-md-7"></div>'+
                        '<div class="col-md-5" style="text-align: right;" >'+
                        '<ul class="pagination">' +
                        '<li class={{(!totalPage||!pageNums||selectedPage=="0")?"disabled":""}}><a href="javascript:void(0)" ng-click="handlePageClick(\'Previous\')" >&laquo;</a></li>' +
                        '<li  ng-repeat="n in pageNums" class={{selectedPage+1==n?"active":""}}><a href="javascript:void(0)"ng-click="handlePageClick(n)">{{n}}</a></li>' +
                        '<li class={{(!totalPage||!pageNums||totalPage-1==selectedPage)?"disabled":""}}><a href="javascript:void(0)" ng-click="handlePageClick(\'Next\')">&raquo;</a></li>' +
                        '</ul>'+
                        '</div>'+
                        '</div>';
                },
                link : function(scope, element, attrs,ctrl) {
                    scope.handlePageClick=function (pageNo) {
                        if(!scope.pageNums||(pageNo+"").indexOf('..')>=0||scope.selectedPage==pageNo-1||!scope.totalPage){
                            return;
                        }
                        if(pageNo=='Previous'){
                            if(scope.selectedPage==0){
                                return;
                            }
                            pageNo=scope.selectedPage;
                        }else if(pageNo=='Next'){
                            if(scope.selectedPage+1==scope.totalPage){
                                return;
                            }
                            pageNo=scope.selectedPage+2;
                        }else{

                        }
                        ctrl._queryData({pageNo:pageNo-1,pageSize:10});
                    }


                },
            };
        });
}
